package com.yidiandao.business.utils;

import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.view.View;

import com.yidiandao.business.constant.Common;

/**
 * 动画工具
 */
public class AnimatorUtils {

    /**
     * 显示动画
     */
    public static void showToTopAnimator(
            View view, boolean isVisibility, boolean isOpening, AnimatorListenerAdapter adapter) {

        // 显示动画完成，只做一次
        if (/*isVisibility ||*/ isOpening) {
            return;
        }

        if (view.getVisibility() == View.VISIBLE) {
            return;
        }

        if (view.getVisibility() == View.GONE) {
            view.setVisibility(View.VISIBLE);
        }

        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 0, 1);
        animator.setDuration(Common.DEFAULT_ANIMATOR_DURATION);
        animator.addListener(adapter);
        animator.start();
    }

    public static void hideToTopAnimator(
            View view, boolean isVisibility, boolean isHiding, AnimatorListenerAdapter adapter) {
        if (/*!isVisibility ||*/ isHiding) {
            return;
        }

        if (view.getVisibility() == View.GONE) {
            return;
        }
        if (view.getVisibility() == View.VISIBLE) {
            view.setVisibility(View.GONE);
        }
        // 如果动画已经关闭，下次还是关闭，则不再关闭
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 1, 0);
        animator.setDuration(Common.DEFAULT_ANIMATOR_DURATION);
        animator.addListener(adapter);
        animator.start();
    }
}
